<template>
  <app-page>
    <div class="user_index">
      <div class="user_content">
        <div class="title">账户设置</div>
        <div class="user_vip">
          <div class="vip_detail">
            <img src="@/assets/user/icon-vip.png" style="width: 40px; height: 40px" alt="" />
            <div class="vip_text">会员有效期至：2024.12.31</div>
          </div>
        </div>
        <el-scrollbar height="calc( 100vh - 250px )">
          <div class="user_list">
            <div class="user_item">
              <div class="user_item_label">头像</div>
              <div class="user_item_content">
                <!-- {{ userStore.user ? getFirstLetter(userStore.user.user_name) : '' }} -->
                <div class="avatar"></div>
              </div>
            </div>
            <div class="user_item">
              <div class="user_item_label">用户名</div>
              <div class="user_item_content" style="flex: 1">
                {{ userStore.user ? userStore.user.user_name : '' }}
              </div>
              <div class="user_item_button">修改</div>
            </div>
            <div class="user_item">
              <div class="user_item_label">用户ID</div>
              <div class="user_item_content" style="flex: 1">
                {{ userStore.user ? userStore.user.uid : '' }}
              </div>
              <div class="user_item_button">复制</div>
            </div>
            <div
              v-if="plantcombinslist.length > 0"
              class="user_item"
              style="min-height: 88px; height: auto; align-items: flex-start"
            >
              <div class="user_item_label" style="height: 88px; display: flex; align-items: center">
                绑定账号
              </div>
              <div class="binding_list">
                <div class="binding_item" v-for="(item, index) in plantcombinslist" :key="index">
                  <div class="binding_item_content" style="flex: 1">
                    <img
                      :src="item.plant_icon"
                      style="width: 80px; height: 80px; border-radius: 18px"
                      alt=""
                    />
                    <div class="binding_item_text">
                      <div class="plantname">{{ item.plant_name }}</div>
                      <div class="binding_id" v-if="userStore.userdata.combins[item.plant_code]">
                        <div>账号：{{ userStore.userdata.combins[item.plant_code].user_name }}</div>
                        <div style="margin-left: 24px; color: #ff7900; cursor: pointer">解除绑定</div>
                      </div>
                    </div>
                  </div>
                  <div
                    v-if="!userStore.userdata.combins[item.plant_code]"
                    class="binding_item_button"
                    disabled
                    @click="clickbinding(item)"
                  >
                    绑定
                  </div>
                  <div v-else class="binding_item_button" style="opacity: 0.7; cursor: no-drop">已绑定</div>
                </div>
              </div>
            </div>
            <div class="user_item">
              <div class="user_item_label">退出当前账号，不再使用</div>
              <div class="user_item_button" style="flex: 1">
                <div class="loginout" @click="userStore.logout()">退出登录</div>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </div>

    <BindingDialog v-model="showbilding" :activebilding="activebilding" />
  </app-page>
</template>

<script setup>
import http from '@/utils/uc-http'
import BindingDialog from './BindingDialog.vue'
import { onMounted } from 'vue'
const userStore = useUserStore()

const plantcombinslist = ref([])
const showbilding = ref(false)
const activebilding = ref({})

const getFirstLetter = str => {
  return str.trim().charAt(0)
}

const clickbinding = row => {
  showbilding.value = true
  activebilding.value = row
}

const getplantcombins = () => {
  let body = {
    plant_code: userStore.packageJs.plantcode,
  }
  http.post('/plant_combins', body).then(res => {
    plantcombinslist.value = res
  })
}

onMounted(() => {
  getplantcombins()
})
</script>

<style lang="scss" scoped>
.user_index {
  width: 100%;
  height: 100%;
  background-color: #fff;
  display: flex;
  padding: 40px 0;
  justify-content: center;
}

.user_content {
  width: 1000px;
  display: flex;
  flex-direction: column;

  .title {
    font-size: 20px;
    font-weight: 600;
    color: #333;
  }

  .user_vip {
    margin-top: 16px;
    width: 100%;
    height: 96px;
    display: flex;
    align-items: flex-end;

    background-image: url('@/assets/user/vip-bg.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;

    .vip_detail {
      height: 84px;
      width: 100%;
      display: flex;
      align-items: center;
      margin-left: 24px;

      .vip_text {
        margin-left: 16px;
        font-size: 16px;
        color: #333;
      }
    }
  }

  .user_list {
    margin-top: 40px;
    display: flex;
    flex-direction: column;

    .user_item {
      width: 100%;
      height: 88px;
      display: flex;
      align-items: center;

      border-bottom: 1px solid #ebebeb;

      .user_item_label {
        width: 280px;
        font-size: 16px;
        color: #333;
      }

      .user_item_content {
        font-size: 16px;
        font-weight: 600;
        color: #333;

        .avatar {
          width: 56px;
          height: 56px;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 32px;
          font-weight: 600;
          color: #fff;
          cursor: pointer;

          background-image: url('@/assets/touxiang.png');
          background-size: cover;
          background-position: center;
          background-repeat: no-repeat;
        }
      }

      .user_item_button {
        font-size: 16px;
        color: #ff7900;
        cursor: pointer;
        display: flex;
        justify-content: end;

        .loginout {
          width: 108px;
          height: 40px;
          background-color: #f5f5f5;
          font-size: 16px;
          color: #333;
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;
        }
      }
    }
  }
}

.binding_list {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: calc(100% - 280px);
}

.binding_item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 88px;
}

.binding_item_content {
  display: flex;
  align-items: center;

  .binding_item_text {
    display: flex;
    flex-direction: column;
    margin-left: 12px;

    .plantname {
      font-size: 16px;
      font-weight: 600;
      color: #333;
    }

    .binding_id {
      display: flex;
      margin-top: 8px;
      font-size: 14px;
      color: #333;
    }
  }
}

.binding_item_button {
  width: 108px;
  height: 40px;
  background-color: #f5f5f5;
  font-size: 16px;
  color: #333;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}
</style>
